package com.chencs.app.special.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.app.AppCommon;
import com.chencs.app.special.service.AppSpecialService;
import com.chencs.biz.product.model.SimpleProductResponse;
import com.chencs.vo.biz.product.ProductModel;
import com.chencs.vo.biz.special.SpecialModel;
import com.chencs.vo.net.request.ProductRequest;
import com.chencs.vo.net.request.RequestBase;
import com.chencs.vo.net.request.SpecialRequest;
import com.chencs.vo.net.response.SpecialListResponse;
import com.chencs.vo.net.response.SpecialResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by fangzehua on 2017/7/12.
 */

@RestController
@RequestMapping("/app/special")
public class AppSpecialController {


    @Autowired
    private AppSpecialService appSpecialService;

    /**
     * 获取专题列表
     * @return
     */
    @RequestMapping(value = "/getSpecialList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getSpecialList(@RequestBody RequestBase request){
        SpecialListResponse  response = new SpecialListResponse();
        List<SpecialModel> list = null;
        try {
            list = appSpecialService.getSpecialList(request);
            if(list!=null){
                response.setSpecials(list);
                response.setCode(AppCommon.SUCCESS);
            }else{
                response.setCode(AppCommon.NULL);
            }
            return  JSON.toJSONString(response);
        }catch (Exception e){
            response.setCode(AppCommon.ERROR);
            return  JSON.toJSONString(response);
        }
    }

    /**
     * 获取專題詳情
     * @return
     */
    @RequestMapping(value = "/findSpecial", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findSpecial(@RequestBody SpecialRequest request){
        SpecialResponse response = new SpecialResponse();
        SpecialModel model = null;
        try {
            model = appSpecialService.findSpecial(request.getId());
            if(model!=null){
                response.setSpecials(model);
                response.setCode(AppCommon.SUCCESS);
            }else{
                response.setCode(AppCommon.NULL);
            }
        }catch (Exception e){
            response.setCode(AppCommon.ERROR);
        }
        return  JSON.toJSONString(response);
    }


    /**
     *
     * @return
     */
    @RequestMapping(value = "/findSpecialProBySid", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findSpecialProBySid(@RequestBody SpecialRequest request){
        SpecialResponse response = new SpecialResponse();
        List<ProductModel> model = null;
        try {
            model = appSpecialService.findSpecialProBySid(request.getId());
            if(model!=null){
                response.setProducts(model);
                response.setCode(AppCommon.SUCCESS);
            }else{
                response.setCode(AppCommon.NULL);
            }
        }catch (Exception e){
            response.setCode(AppCommon.ERROR);
        }
        return  JSON.toJSONString(response);
    }
}
